<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 9/22/14
 * Time: 8:21 PM
 */

namespace Backend\Modules\Video\Forms;
use Common\Forms\BaseForm;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\TextArea;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Radio;
use Phalcon\Forms\Element\File;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Identical;
use Phalcon\Validation\Validator\StringLength;
use Phalcon\Validation\Validator\Confirmation;
use Phalcon\Validation\Validator\Between;
use Backend\Modules\Video\Models\National;
use Backend\Modules\Video\Models\VideoCategory;
use Common\Library\Common as LibraryCommon;

class FormCreateVideoGroup extends BaseForm
{
    public function initialize($entity = null , $options = null)
    {
        $id = new Hidden('id');
        $this->add($id);

        $name = new Text('name' , array(
            'placeholder' => '',
            'class' => 'forms-control col-xs-12',
            //'name'  => 'name'
        ));
        $name->setLabel("Tên tiếng anh");

        $name->addValidators(array(
            new PresenceOf(array(
                'message' => 'name is required'
            )),
            new  StringLength(array(
                'max' => 125 ,
                'message' => 'name is too long'
            ))
        ));
        $this->add($name);

        $nameVN = new Text('name_vn' , array(
            'placeholder' => '',
            'class' => 'forms-control col-xs-12',
            //'name'  => 'name'
        ));
        $nameVN->setLabel("Tên tiếng việt");

        $nameVN->addValidators(array(
            new PresenceOf(array(
                'message' => 'name is required'
            )),
            new  StringLength(array(
                'max' => 125 ,
                'message' => 'name is too long'
            ))
        ));
        $this->add($nameVN);


        /*
         * Video Category
         */
        $category = $national = new Select('category_id' , VideoCategory::fetchCategoryTree(0 , '' , '')  , array('using' => array('id' , 'name') , 'useEmpty' => true , 'emptyText' => 'Chọn thể loại' , 'emptyValue' => '' ,  'class' => 'forms-control col-xs-12') );
        $category->setLabel('Thể loại');

        $category->addValidators(array(
            new PresenceOf(array(
                'Message' => 'category is required'
            ))
        ));
        $this->add($category);


        $description = new TextArea('description' , array(
            'class' => 'forms-control col-xs-12',
            'maxlength' =>  1000 ,
            'row'       => 10
        ));
        $description->setLabel('Mô tả');
        $this->add($description);

        $totalEpisode = new Text('total_episode' ,  array('class' => 'forms-control col-xs-12') );
        $totalEpisode->setLabel('Tổng số tập');
        $this->add($totalEpisode);

        $national = new Select('national_id' , National::find() , array('using' => array('id' , 'name') , 'useEmpty' => true , 'emptyText' => 'Chọn quốc gia' , 'emptyValue' => 0 ,  'class' => 'forms-control col-xs-12') );
        $national->setLabel('Quốc gia');
        $this->add($national);

        $yearProduct = new Select('year_product' , LibraryCommon::listYear()  , array( 'useEmpty' => true , 'emptyText' => 'Chọn năm sản xuất' , 'emptyValue' => 0 ,  'class' => 'forms-control col-xs-12') );
        $yearProduct->setLabel('Năm sản xuất');
        $this->add($yearProduct);

        $yearPublish = new Select('year_publish' , LibraryCommon::listYear()  , array( 'useEmpty' => true , 'emptyText' => 'Chọn năm phát hành' , 'emptyValue' => 0 ,  'class' => 'forms-control col-xs-12') );
        $yearPublish->setLabel('Năm phát hành');
        $this->add($yearPublish);

        $file = new File('file' , array(
            'id' => ''
        ));
        $file->setLabel('Hình đại diện');
        $this->add($file);

        $filePoster = new File('file_poster' , array(
            'id' => 'file_poster',
            'name' => 'file_poster'
        ));
        $filePoster->setLabel('Hình Poster');
        $this->add($filePoster);


        $album = new File('album' , array(
            'id' => '',
            'name'  => 'files[]',
            'multiple' => true
        ));
        $album->setLabel('Album ảnh');
        $this->add($album);

        /*$status = new Select('status' ,
            array(
                'A' => 'Hoạt động' ,
                'H' => 'Ẩn' ,
                'D' => 'Vô hiệu hóa'
            ) ,
            array(
                'useEmpty' => true ,
                'emptyText' => 'Chọn trạng thái' ,
                'emptyValue' => '' ,
                'class' => 'forms-control col-xs-12',
            )
        );
        $status->addValidator(new PresenceOf(array(
            'Message' => 'Status is required'
        )));
        $this->add($status);*/


        // CSRF
        $csrf = new Hidden('csrf');

        $csrf->addValidator(new Identical(array(
            'value' => $this->security->getSessionToken(),
            'message' => 'CSRF validation failed'
        )));

        $this->add($csrf);

        // Sign Up
        $this->add(new Submit('submit', array(
            'class' => 'btn btn-primary'
        )));

    }

    public function messages($name)
    {
        if($this->getMessagesFor($name))
        {
            foreach ($this->getMessagesFor($name) as $message)
            {
                $this->flash->error($message);
            }
        }
    }
} 